<?php

/*
PAGE CLASS
Copyright (C) 2004-2005 Harry Wang
GNU General Public License 321
This program is free software; you can
redistribute it and/or modify it under the
terms of the GNU General Public License as
published by the Free Software Foundation;
either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that
it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU
General Public License along with this
program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
/**
 * 數據庫分頁類
 * 這個數據庫分頁類可以使用在帶有ADODB的數據庫中
 * @author		Weizhen Wang <harry34010493@gmail.com>
 * @version	$Id: page.inc.php,v 1.19 2007/10/15 10:00:05 nbx Exp $
 * @example  /inc/example/page.example.php
 * 使用簡介:
 * $p = new ShowPage(根据?($DB), 表名字, 分頁條數, 具体sql語句(可選));		//建立新對象
 * $p->SetPageVar(pagecount);	//設置翻頁的標示，默認是'p'
 * $pVar = $p->GetPageVar();    //返回當前PageVar
 * $p->GetTotalPageArray();     //生成下拉菜單
 * $p->GetTotalPage();          //返回總頁數
 * $p->GetLeftSide();			//返回向左的箭頭
 * $p->GetRightSide();			//返回向右的箭頭
 * $p->GetCurrentPage();		//返回當前頁面
 * $p->GetLimit();				//返回每頁顯示條數			
 * $p->GetRowFrom();			//返回Select語句用的當前Row的位置    E.G."SELECT * FROM SupplierLogin LIMIT " . $p->GetRowFrom() . ", " . $p->GetLimit();
 * $p->SetVar();				//設置同時需要傳送的參數
 */
class ShowPage
{
	/**
	 * 左翻頁
	 * @var string
	 */
	var $outputLeftSide;
	
	/**
	 * 右翻頁
	 * @var string
	 */
	var $outputRightSide;
	var $file;
	var $class = 'p_num';
	/**
	 * 翻頁參數
	 * @var string
	 */
	var $pVar = "p";
	
	/**
	 * 當前頁
	 * @var int
	 */
	var $curr;
	var $varstr;
	var $tPage;
	
	/**
	 * 數據庫對象
	 * @var DB
	 */
	var $DB;
	
	/**
	 * 數據庫表名
	 * @var string
	 */
	var $table;
	
	/**
	 * 每頁顯示的數據條數
	 * @var string
	 */
	var $limit;
	
	/**
	 * SQL語句
	 * @var string
	 */
	var $sqlQuery;
	var $tPageArray;
	var $total;
    var $tRow;
	var $target = '_self';
	var $maxPage = 0;
	/**
	* Paging style "option" or "listing"
	*/
	var $style = "listing";

	function ShowPage($DB, $limit, $sqlQuery = "", $maxpage=0, $pVar='p') {
		$this->DB       = $DB;
		$this->table	= $table;
		$this->limit	= $limit;
		$this->sqlQuery	= $sqlQuery;
		$this->target_str = 'target="'.$this->target.'"';
		if(IS_WAP)$this->target_str = NULL;
		$this->maxPage = ($maxpage>0)?$maxpage:0;
		$tmpGET = $_GET;
		$this->pVar = $pVar;
		unset($tmpGET[$this->pVar]);
		
		$this->setVar($tmpGET);


		$tmpQuery = $sqlQuery;

		$sql = preg_replace("/select (.+) from/i", "select count(*) from", $tmpQuery);
		$rs = $DB->query($sql);
		//$rs = $DB->query("SELECT FOUND_ROWS()");
		$tPage = $rs->fetchColumn();
		

        $this->tRow = $tPage;

		$this->tPage = ($this->maxPage>0)?$this->maxPage:ceil($tPage / $this->limit);
		if ($this->tPage > 0)
		{
			for ($i = 1; $i < $this->tPage + 1; $i++)
			{
				$this->tPageArray[] = $i;
			}
		}
		else
		{
			$this->tPageArray[] = 0;
		}
	}

    /**
	 * 設置翻頁參數
	 * @param string $pvar  翻頁參數
	 */
	function SetPageVar($pvar = 'p')
	{    
        // set type turnpage parameter (E.G. http://www.aa.com/index.php?p=1)
		$this->pVar = $pvar;
	}

	/**
	 * 返回總行數
	 * @return  int
	 */

	function GetTotalRow()
	{
		return $this->tRow;
	}


    /**
	 * 返回翻頁參數
	 * @return string
	 */
	function GetPageVar()
	{
		return $this->pVar;
	}


    /**
	 * 返回當前頁數
	 * @return int
	 */
	function GetCurrentPage()
	{
		return  $_GET[$this->pVar];
	}


    /**
	 * 返回總頁數數組
	 * @return array
	 */
	function GetTotalPageArray()
	{
		return $this->tPageArray;
	}


    /**
	 * 返回總頁數
	 * @return int
	 */
	function GetTotalPage()
	{
		return $this->tPage;
	}


    /**
	 * 返回每頁顯示條數
	 * @return int
	 */
	function GetLimit()
	{   
		return $this->limit;
	}

	/**
	 * 返回數據行數
	 * @return  int
	 */
	function GetRowFrom()
	{
		if ($this->GetCurrentPage() <= 1)
		{
			return 0;
		}
		else
		{
			return ($this->GetCurrentPage() - 1) * $this->GetLimit();
		}
	}


	/**
	 * 返回朝左翻
	 * @return  string
	 */
	function GetLeftSide()
	{
		$current = $_GET[$this->pVar];
		
		
		if (!$current)
		{
			$current = 1;
		}

		if ($current<1)
		{
			$current = 1;
		}

		if ($current>$this->tPage)
		{
			$current = $this->tPage;
		}

		if ($this->tPage > 0)
		{
			$this->outputLeftSide = '';
			if ($current > 10)
			{
				$this->outputLeftSide = '<a href="'.$this->file.'?'.($this->varstr).$this->pVar.'=1'.'" title="First Page" class="'.$this->class.'" '.$this->target_str.'>第一页</a> ';
			}
            if ($current > 1)
			{
				$this->outputLeftSide .= '<a href="'.$this->file.'?'.($this->varstr).$this->pVar.'='.($current-1).'" title="Previous Page" class="'.$this->class.'" '.$this->target_str.'>上一页</a> ';
			}
		}
		
		return str_replace('"', "'", $this->outputLeftSide);
	}

	
	/**
	 * 返回朝右翻
	 * @return  string
	 */
	function GetRightSide() {
		$current = $_GET[$this->pVar];

		if (!$current) {
			$current = 1;
		}

		if ($current>$this->tPage) {
			$current = $this->tPage;
		}

		if ($current<1) {
			$current = 1;
		}

		if ($this->tPage > 0) {     
			$this->outputRightSide = '';
			if ($current<$this->tPage) {
				$this->outputRightSide = '<a href="'.$this->file.'?'.($this->varstr).$this->pVar.'='.($current+1).'" title="Next Page" class="'.$this->class.'" '.$this->target_str.'>下一页</a> ';
			}
            if ($this->tPage>10 && ($this->tPage-$current)>=10 ) {
				$this->outputRightSide .= '<a href="'.$this->file.'?'.($this->varstr).$this->pVar.'='.$this->tPage.'" title="Last Page" class="'.$this->class.'" '.$this->target_str.'>最后页</a> ';
			}
		}
		
		return str_replace('"', "'", $this->outputRightSide);
	}

	
	/**
	 * 返回一個完整的翻頁選項
	 * 包含了向左翻(<< <), 直接選擇頁面數字， 向右翻(> >>), 和所需要的Javascript
	 * @return string
	 */
	function GetFullSide() {
		
		if($this->style == "options")
		{
			$sCenter = "<select onChange='fnJump(this.value)' name='select'>\n";
			$pages = $this->GetTotalPageArray();
			foreach($pages as $page) {
				$selected = "";
				if($page == $this->GetCurrentPage())$selected = "selected";
				$sCenter .= "	<option value=\"$page\" $selected>$page</option>\n";
			}
			$sCenter .= "</select>\n";
			$sScript = $this->GetTurnPageScript();
		}
		else
		{
			$sCenter = "";
			$pages = $this->GetTotalPageArray();
			foreach($pages as $page) {
				if($this->GetCurrentPage()-$page>=4)continue;
				if($page >= $this->GetCurrentPage()+4)break;
				$sCenter .= "<a href=\"".$_SERVER['PHP_SELF']."?".($this->varstr).$this->GetPageVar()."=$page"."\" class='".$this->class."' $this->target_str>";
				if($page == $this->GetCurrentPage())
				{
					$sCenter .= "<b>".$page."</b>";
				}else $sCenter .= $page;
				$sCenter .= "</a>\n";
			}
			$sScript = "";
		}
		$sLeftSide = $this->GetLeftSide();
		$sRightSide = $this->GetRightSide();
		return ($sLeftSide ."\n". $sCenter ."\n". $sRightSide ."\n". $sScript);
	} // end func

	
	/**
	 * 返回Sql語句
	 * @return string
	 */
	function GetNewSql() {
		return $this->sqlQuery . " LIMIT " . $this->GetRowFrom() . ", " . $this->GetLimit();
	} // end func

	
	/**
	 * 返回翻頁時所需的Javascript
	 * @return string
	 */
	function GetTurnPageScript() {
		return	"<SCRIPT LANGUAGE=\"JavaScript\">\n" . 
				"<!--\n" . 
				"function fnJump(page)\n" . 
				"{\n" .
				"location.href='".$_SERVER['PHP_SELF']."?" . $this->GetPageVar() ."='+page" . "+'".($this->varstr) . "';\n" . 
				"}\n" . 
				"//-->\n" . 
				"</SCRIPT>\n";
	} // end func

	
	/**
	 * 設置其它參數
	 * 設置翻頁時需要通過用GET方法一同傳送過去的其他參數
	 * @param  array $data
	 */
	function setVar($data) {    
		// set the turnpage addition value need to transfer by get mode
		
		foreach ($data as $k=>$v)
		{

			$value = ($v)?'='.urlencode($v):'';
			//if($k>0&&$k<count($data)-1)$this->varstr;
			
			if($v==='')$this->varstr.=$k.'/&amp;';
			else $this->varstr.=$k.$value.'&amp;';
			
		}
		$this->varstr = str_replace('//', '/', $this->varstr);
	}


}





class ajax_ShowPage extends ShowPage
{
	var $func;
	var $currentpage;
	function ajax_ShowPage($DB, $table, $limit, $sqlQuery = "", $function='ShowPage([page])', $currentpage=1, $target='')
	{
		$this->ShowPage($DB, $table, $limit, $sqlQuery);
		$this->func = $function;
		$this->currentpage = ($currentpage)?$currentpage:1;
		$this->target = $target;
	}


	function ajaxGetCurrentPage()
	{
		return $this->currentpage;
	}


	function ajaxGetRowFrom()
	{
		if ($this->ajaxGetCurrentPage() <= 1) {
			return 0;
		}
		else {
			return ($this->ajaxGetCurrentPage() - 1) * $this->GetLimit();
		}
	}

	

	function ajaxGetNewSql()
	{
		return $this->sqlQuery . " LIMIT " . $this->ajaxGetRowFrom() . ", " . $this->GetLimit();
	}

	function ajaxLeftSide() {
		$current = $this->currentpage;
		
		
		if (!$current) {
			$current = 1;
		}

		if ($current<1) {
			$current = 1;
		}

		if ($current>$this->tPage) {
			$current = $this->tPage;
		}

		if ($this->tPage > 1) {            
			if ($current > 10) {
				$this->outputLeftSide = '<a href="##'.$this->target.'" onclick="'.$this->func."('1')".'" title="" class="p_num" target="{$this->target}">第一页</a>&nbsp;';
			}
            if ($current > 1) {
				$this->outputLeftSide = '<a href="##'.$this->target.'" onclick="'.$this->func."(".($current-1).")".'" title="" class="p_num" target="{$this->target}">上一页</a>&nbsp;';
			}
		}
		
		return $this->outputLeftSide;
	}


	function ajaxRightSide() {
		$current = $this->currentpage;
		
		
		if (!$current) {
			$current = 1;
		}

		if ($current<1) {
			$current = 1;
		}

		if ($current>$this->tPage) {
			$current = $this->tPage;
		}

		if ($this->tPage > 1) {            
			if ($current<$this->tPage) {
				$this->outputRightSide = "<a  href='##".$this->target."' onclick=\"{$this->func}('".($current+1)."')\" title=\"Next Page\" class=\"p_num\" target=\"{$this->target}\">下一页</a>&nbsp;";
			}
            if ($this->tPage>10 && ($this->tPage-$current)>=10 ) {
				$this->outputRightSide .= '<a href="##'.$this->target.'" onclick="'.$this->func."('".$this->tPage."')".'" title="Next 10 Page" class="p_num" target="'.$this->target.'">最后</a>';
			}
		}
		
		return $this->outputRightSide;
	}
	
	function ajaxGetFullSide() {
		$sLeftSide = $this->ajaxLeftSide();
		$sRightSide = $this->ajaxRightSide();
		if($this->style == "options")
		{
			$sCenter = "<select onChange='fnJump(this.value)' name='select'>\n";
			$pages = $this->GetTotalPageArray();
			foreach($pages as $page) {
				$selected = "";
				if($page == $this->ajaxGetCurrentPage())$selected = "selected";
				$sCenter .= "	<option value=\"$page\" $selected>$page</option>\n";
			}
			$sCenter .= "</select>\n";
			$sScript = $this->ajaxGetTurnPageScript();
		}
		else
		{
			$sCenter = "";
			$pages = $this->GetTotalPageArray();
			foreach($pages as $page) {
				if($this->GetCurrentPage()-$page==4)$sCenter .= '...';
				if($this->GetCurrentPage()-$page>=4)continue;
				if($page >= $this->GetCurrentPage()+4&&$this->tPage-$page>2)continue;
				$sCenter .= '<a href="##'.$this->target.'" onclick="'.$this->func."('".$page."')".'" class="p_num" target="'.$this->target.'">';
				if($page == $this->GetCurrentPage())
				{
					$sCenter .= "<b>".$page."</b>";
				}else $sCenter .= $page;
				$sCenter .= "</a>\n";
			}
			$sScript = "";
		}
		return ($sLeftSide ."\n". $sCenter ."\n". $sRightSide ."\n". $sScript);
	} 


	function ajaxGetTurnPageScript()
	{
		return	"<SCRIPT LANGUAGE=\"JavaScript\">\n" . 
				"<!--\n" . 
				"function fnJump(page)\n" . 
				"{\n" .
				"	".$this->func."(page);\n" . 
				"}\n" . 
				"//-->\n" . 
				"</SCRIPT>\n";
	} // end func

}
?>